
package uestc.bitman.edxapp.downloaded;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

public class SetVideo {

    private List<String> videoPaths = null;

    private List<String> videoNames = null;

    private List<String> courseNames = null;

    private String mappingPath = null;

    private String rootPath = null;

    boolean isGet = false;

    public SetVideo(String rootPath) {

        File file = new File(rootPath);
        if (!file.exists()) {
            file.mkdir();
        }
        this.courseNames = new ArrayList<String>();
        this.videoNames = new ArrayList<String>();
        this.videoPaths = new ArrayList<String>();
        this.rootPath = rootPath;
        mappingPath = rootPath + "/videoManager.xml";
        loadVideoImformation();
    }

    public void setData(String courseName, String videoName, String videoPath) {

        this.courseNames.add(courseName);
        this.videoNames.add(videoName);
        this.videoPaths.add(videoPath);
    }

    private void loadVideoImformation() {
    	
        if (isGet == false) {
            GetVideo getVideo = new GetVideo(rootPath);
            getVideo.ResolveXMl();

            List<String> existCourseNames = getVideo.getCourseNames();
            List<String> existVideoNames = getVideo.getVideoNames();
            List<String> existVideoPaths = getVideo.getVideoPaths();

            for (int i = 0; i < existCourseNames.size(); i++) {
                this.courseNames.add(existCourseNames.get(i));
                this.videoNames.add(existVideoNames.get(i));
                this.videoPaths.add(existVideoPaths.get(i));
            }
            isGet = true;
        }
    }

    public void deleteVideo(String path,String fileName) {
        if (isGet == false) {
            loadVideoImformation();
            isGet = true;
        }
        for (int i = 0; i < videoPaths.size(); i++) {
            if (fileName.equals(videoPaths.get(i))) {
                courseNames.remove(i);
                videoNames.remove(i);
                videoPaths.remove(i);
                break;
            }
        }
        File file = new File(path + fileName);
        if (file.exists()) {
            file.delete();
        }
        setXml();
    }

    public void setXml() {

        if (!isGet) {
            loadVideoImformation();
        }
        File file = new File(mappingPath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e1) {
            e1.printStackTrace();
        }
        Document doc = builder.newDocument();
        Element root = doc.createElement("Videos");
        doc.appendChild(root);

        Element video = null;
        Element courseName = null;
        Element videoName = null;
        Element videoPath = null;

        for (int i = 0; i < courseNames.size(); i++) {
            video = doc.createElement("Video");
            courseName = doc.createElement("CourseName");
            courseName.setTextContent(courseNames.get(i));
            videoName = doc.createElement("VideoName");
            videoName.setTextContent(videoNames.get(i));
            videoPath = doc.createElement("VideoPath");
            videoPath.setTextContent(videoPaths.get(i));
            video.appendChild(courseName);
            video.appendChild(videoName);
            video.appendChild(videoPath);
            root.appendChild(video);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {

            e.printStackTrace();
        }
        DOMSource domSource = new DOMSource(doc);
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(file);
            StreamResult xmlResult = new StreamResult(os);
            transformer.transform(domSource, xmlResult);
            os.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        Clear();
        isGet = false;
    }

    private void Clear() {

        courseNames.clear();
        videoNames.clear();
        videoPaths.clear();
    }
}
